package show.me.the.money.config.schema;

import java.math.BigDecimal;
import java.util.HashMap;

import show.me.the.money.util.T;


public class TypeValueUtil {
	private static HashMap<String, Integer> types = new HashMap<String, Integer>();

	static {
		types.put("bigDecimal", 1);
		types.put("int", 2);
		types.put("long", 3);
		types.put("short", 4);
		types.put("float", 5);
		types.put("double", 6);
		types.put("string", 7);
		types.put("timestamp", 8);
		types.put("date", 9);
		types.put("time", 10);
		types.put("character", 11);
	}

	public static int getStaticType(String name) {
		return types.get(name);
	}

	public static Object typeValue(String type, String value) {
		Integer tp = types.get(type);
		if (tp == null) {
			return value;
		}
		if (value == null) {
			return value;
		}
		if (value.equals("")) {
			if (tp == 7) {
				return value;
			} else {
				return null;
			}
		}

		Object v = null;
		switch (tp) {
		case 7:
			v = value;
			break;
		case 2:
			v = Integer.valueOf(value);
			break;
		case 3:
			v = Long.valueOf(value);
			break;
		case 4:
			v = Short.valueOf(value);
			break;
		case 5:
			v = Float.valueOf(value);
			break;
		case 6:
			v = Double.valueOf(value);
			break;
		case 11:
			v = Character.valueOf(value.charAt(0));
			break;
		case 8:
			try {
				v = T.parseDateTime(value);
			}
			catch (Exception e) {
			}
			break;
		case 9:
			try {
				v = T.parseDate(value);
			} catch (Exception e) {
			}
			break;
		case 10:
			try {
				v = T.parseTime(value);
			} catch (Exception e) {
			}
			break;
		case 1:
			try {
				v = BigDecimal.valueOf(Double.parseDouble(value));
			} catch (Exception e) {
				v = null;
			}
			break;
		}
		return v;
	}
}
